﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using PDAMain.Forms.RepairWork;
using PDAMain.Forms.ReadingWatermeter;
using System.IO;
using OpenNETCF.Ftp;
using PDAMain.Forms.ReadingAmmeter;
using AlphaMobileControls;
using PDAMain.Forms.InspectionWork;
using PDAMain.Forms.CustomerWork;
 

namespace PDAMain
{
    public partial class FunctionList : AlphaMobileControls.AlphaForm
    {
        public FunctionList()
        {
            InitializeComponent();
        }

        delegate void StringDelegate(string value);

        private FTP m_ftp;
        string[] UploadData = null;
        private void GetUserRole()
        {
            string RoleCode = Identity.RoleCode;
            string[] ArrRole = RoleCode.Split(',');
            UploadData = ArrRole;
            foreach (string strRole in ArrRole)
            {

                //维修工单
                if (strRole.ToUpper() == "ROLE_REPAIRER")
                {
                    //pictureBoxRepair.Image = AlphaImage.CreateFromResource("PDAMain.Image.repair.png");
                    pictureBoxRepair.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair.png");
                    pictureBoxRepair.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");
                    pictureBoxRepair.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair.png");


                    pictureBoxRepair.Enabled = true;
                    //pictureBoxRepair.Visible = true;
                    //pictureBoxRepair_v2.Visible = false;
                }

                //巡检管理
                if (strRole.ToUpper() == "ROLE_INSPECTOR")
                {
                    //pictureBoxInspection.Image = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");
                    pictureBoxInspection.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");
                    pictureBoxInspection.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");
                    pictureBoxInspection.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");
                    pictureBoxInspection.Enabled = true;
                    //pictureBoxInspection.Visible = true;
                    //pictureBoxInspection_v2.Visible = false;
                }
                //客服工单
                if (strRole.ToUpper() == "ROLE_CUSTOM_SERVICE")
                {
                    //pictureBoxCustomer.Image = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");
                    pictureBoxCustomer.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");
                    pictureBoxCustomer.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");
                    pictureBoxCustomer.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");
                    pictureBoxCustomer.Enabled = true;
                    //pictureBoxCustomer.Visible = true;
                    //pictureBoxCustomer_v2.Visible = false;
                }

                //电表抄表
                if (strRole.ToUpper() == "ROLE_AM_READING")
                {
                    //pictureBoxAm.Image = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");
                    pictureBoxAm.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");
                    pictureBoxAm.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");
                    pictureBoxAm.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");
                    pictureBoxAm.Enabled = true;
                    //pictureBoxAm.Visible = true;
                    //pictureBoxAm_v2.Visible = false;
                }

                //水表抄表
                if (strRole.ToUpper() == "ROLE_WATER_READING")
                {
                    //pictureBoxWater.Image = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");

                    pictureBoxWater.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");
                    pictureBoxWater.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");
                    pictureBoxWater.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");
                    pictureBoxWater.Enabled = true;
                    //pictureBoxWater.Visible = true;
                    //pictureBoxWater_v2.Visible = false;
                }

                //系统设置
                if (strRole.ToUpper() == "ROLE_SUPER")
                {
                    pictureBoxRepair.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair.png");
                    pictureBoxRepair.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");
                    pictureBoxRepair.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair.png");

                    pictureBoxInspection.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");
                    pictureBoxInspection.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");
                    pictureBoxInspection.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");

                    pictureBoxCustomer.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");
                    pictureBoxCustomer.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");
                    pictureBoxCustomer.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");

                    pictureBoxAm.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");
                    pictureBoxAm.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");
                    pictureBoxAm.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");

                    pictureBoxWater.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");
                    pictureBoxWater.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");
                    pictureBoxWater.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");

                    pictureBoxSysConfig.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig.png");
                    pictureBoxSysConfig.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig_v2.png");
                    pictureBoxSysConfig.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig.png");

                    pictureBoxSysUpdate.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem.png");
                    pictureBoxSysUpdate.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");
                    pictureBoxSysUpdate.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem.png");




                   /* pictureBoxInspection.Image = AlphaImage.CreateFromResource("PDAMain.Image.inspection.png");
                    pictureBoxCustomer.Image = AlphaImage.CreateFromResource("PDAMain.Image.customer.png");
                    pictureBoxAm.Image = AlphaImage.CreateFromResource("PDAMain.Image.amMeter.png");
                    pictureBoxWater.Image = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter.png");*/

                    //pictureBoxSysConfig.Image = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig.png");
                   // pictureBoxSysUpdate.Image = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem.png");


                    pictureBoxSysUpdate.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem.png");


                    pictureBoxRepair.Enabled = true;
                    pictureBoxInspection.Enabled = true;
                    pictureBoxCustomer.Enabled = true;
                    pictureBoxAm.Enabled = true;
                    pictureBoxWater.Enabled = true;
                    pictureBoxSysConfig.Enabled = true;
                    pictureBoxSysUpdate.Enabled = true;

                    //pictureBoxWater.Visible = true;
                    //pictureBoxAm.Visible = true; 
                    //pictureBoxRepair.Visible = true;
                    //pictureBoxCustomer.Visible = true;
                    //pictureBoxInspection.Visible = true;
                   // sysConfigpictureBox.Visible = true;
                    //pictureBoxUpdate.Visible = true;

                   // pictureBoxRepair_v2.Visible = false;
                   // pictureBoxInspection_v2.Visible = false;
                   // pictureBoxCustomer_v2.Visible = false;
                   // pictureBoxAm_v2.Visible = false;
                   // pictureBoxWater_v2.Visible = false;
                   // sysConfigpictureBox_v2.Visible = false;
                   // pictureBoxUpdate_v2.Visible = false;

                }
            }
        }
        
        /// <summary>
        /// 跳转客服工单页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxCustomer_Click(object sender, EventArgs e)
        {
            CustomerList list = new CustomerList();
            list.Show();
        }

       /// <summary>
       /// 跳转抄电表页面
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void pictureBoxAm_Click(object sender, EventArgs e)
        {
            AmmeterReadingList ARL = new AmmeterReadingList();
            ARL.Show();
        }
        /// <summary>
        /// 跳转抄水表页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxWater_Click(object sender, EventArgs e)
        {
          
            WaterReadingList WRL = new WaterReadingList();
            WRL.Show();
        }

        /// <summary>
        /// 跳转巡检页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxInspection_Click(object sender, EventArgs e)
        {
            InspectionList list = new InspectionList();
            list.Show();
        }
        
        /// <summary>
        /// 跳转维修工单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxRepair_Click(object sender, EventArgs e)
        {
           
            RepairWorkList list = new RepairWorkList();
            list.Show();
        }
        /// <summary>
        ///跳转系统配置页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxSysConfig_Click(object sender, EventArgs e)
        {
            Config showForm = new Config();
            showForm.Show();
            //this.Close();
        }
        
        /// <summary>
        /// 跳转修改密码页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxModifyPassword_Click(object sender, EventArgs e)
        {
            ModifyPassword showForm = new ModifyPassword();
            showForm.Show();
        }
        /// <summary>
        /// 跳转系统升级页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxSysUpdate_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxLoginOut_Click(object sender, EventArgs e)
        {
            DialogResult dr;
            dr = MessageBox.Show("你确定要退出登录么？", "MessageBox", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.OK)
            {
                DataHelper.UserDao.logout();
                //Application.Exit();
                this.Close();
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                DataHelper.SynchronizationDataDao.UploadData();
                foreach (string strRole in UploadData)
                {
                    //维修工单
                    if (strRole.ToUpper() == "ROLE_REPAIRER")
                    {
                        DataHelper.RepairWorkDao.download();
                    }

                    //巡检管理
                    if (strRole.ToUpper() == "ROLE_INSPECTOR")
                    {
                        DataHelper.InspectionWorkDao.updateDataFromWebservice();
                    }
                    //客服工单
                    if (strRole.ToUpper() == "ROLE_CUSTOM_SERVICE")
                    {
                        DataHelper.CustomerWorkDao.updateDataFromWebservice();
                    }

                    //电表抄表
                    if (strRole.ToUpper() == "ROLE_AM_READING")
                    {

                    }

                    //水表抄表
                    if (strRole.ToUpper() == "ROLE_WATER_READING")
                    {

                    }

                    //系统设置
                    if (strRole.ToUpper() == "ROLE_SUPER")
                    {
                        DataHelper.InspectionWorkDao.updateDataFromWebservice();
                        DataHelper.CustomerWorkDao.updateDataFromWebservice();
                        DataHelper.RepairWorkDao.download();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void FunctionList_Load(object sender, EventArgs e)
        {
            /* jimxu*/
            GetUserRole();
            timer1_Tick(null,null);
            timer1.Enabled = true;
            timer1.Interval = ConfigHelper.GetTime();
           
           /* pictureBoxLoginOut.Image = AlphaImage.CreateFromResource("PDAMain.Image.loginOut.png");
            pictureBoxModifyPassword.Image = AlphaImage.CreateFromResource("PDAMain.Image.ModifyPassword.png");

            pictureBoxRepair.Image = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");
            pictureBoxInspection.Image = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");
            pictureBoxCustomer.Image = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");
            pictureBoxAm.Image = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");
            pictureBoxWater.Image = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");

            pictureBoxSysConfig.Image = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig_v2.png");
           // pictureBoxSysUpdate.Image = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");
            pictureBoxSysUpdate.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");*/

            pictureBoxRepair.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");
            pictureBoxRepair.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");
            pictureBoxRepair.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.repair_v2.png");

            pictureBoxInspection.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");
            pictureBoxInspection.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");
            pictureBoxInspection.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.inspection_v2.png");

            pictureBoxCustomer.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");
            pictureBoxCustomer.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");
            pictureBoxCustomer.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.customer_v2.png");

            pictureBoxAm.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");
            pictureBoxAm.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");
            pictureBoxAm.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.amMeter_v2.png");

            pictureBoxWater.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");
            pictureBoxWater.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");
            pictureBoxWater.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.waterMeter_v2.png");

            pictureBoxSysConfig.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig_v2.png");
            pictureBoxSysConfig.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig_v2.png");
            pictureBoxSysConfig.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.SystemConfig_v2.png");

            pictureBoxSysUpdate.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");
            pictureBoxSysUpdate.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");
            pictureBoxSysUpdate.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.updateSystem_v2.png");

            pictureBoxModifyPassword.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.ModifyPassword.png");
            pictureBoxModifyPassword.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.ModifyPassword_v2.png");
            pictureBoxModifyPassword.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.ModifyPassword.png");

            pictureBoxLoginOut.BackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.loginOut.png");
            pictureBoxLoginOut.ActiveBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.loginOut_v2.png");
            pictureBoxLoginOut.DisabledBackgroundImage = AlphaImage.CreateFromResource("PDAMain.Image.loginOut.png");

            //alphaPictureBox1.Image = AlphaImage.CreateFromResource("PDAMain.Image.massagebackgroud.jpg");
            //alphaPictureBox1.Text = "25";
            //pictureBoxSysConfig.Enabled = true;
            //pictureBoxSysUpdate.Enabled = true;
            
            GetUserRole();
        }

        void m_ftp_Connected(FTP source)
        {
            // when this happens we're ready to send command
            OnResponse("Connected.");
            ConnectThread();
        }

        void m_ftp_ResponseReceived(FTP source, FTPResponse Response)
        {
            OnResponse(Response.Text);
        }

        private void OnResponse(string response)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new StringDelegate(OnResponse), new object[] { response });
                return;
            }
            ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToShortTimeString(), response });
            status.Items.Insert(0, item);
            status.Columns[1].Width = -1;
        }

        private void ConnectServer()
        {
            OnResponse("Connecting");
            m_ftp = new FTP("10.134.97.80");
            m_ftp.Port = 1010;
            m_ftp.ConnectionTimeout = 5000;
            m_ftp.ResponseReceived += new FTPResponseHandler(m_ftp_ResponseReceived);
            m_ftp.Connected += new FTPConnectedHandler(m_ftp_Connected);
            m_ftp.BeginConnect("F3219946", "Foxconn7ujm&UJM");
        }

        private void ConnectThread()
        {
            FTPFiles files = m_ftp.EnumFiles();
            DirectoryInfo Folder = new DirectoryInfo(TakePicture.imagePath);
            foreach (DirectoryInfo NextFolder in Folder.GetDirectories())
            {
                bool Is_CreateDirectory = true;
                if (files != null)
                {
                    foreach (FTPFile file in files)
                    {
                        if (NextFolder.Name == file.Name)
                        {
                            Is_CreateDirectory = false;
                            break;
                        }
                    }
                }
                if (Is_CreateDirectory)
                {
                    m_ftp.CreateDirectory(NextFolder.Name);
                }
                foreach (FileInfo NextFile in NextFolder.GetFiles())
                {
                    //MessageBox.Show(NextFile.FullName);
                    FileStream stream = File.OpenRead(NextFile.FullName);
                    m_ftp.SendFile(stream, NextFile.Name, NextFolder.Name);
                    stream.Close();
                }/**/
            }/*
            FileStream stream = File.OpenRead("\\Storage Card\\IMG_5837.JPG");
            m_ftp.SendFile(stream, "IMG_5837.JPG", "new");
            stream.Close();*/
        }

        private void FunctionList_Closed(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

       

       



     


      
    }
}